﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KylinORM.Aspect
{
    /// <summary>
    /// 实体特性
    /// </summary>
    [Serializable]
    [AttributeUsage(AttributeTargets.Class)]
    public class EntityAttribute : Attribute
    {
        /// <summary>
        /// 创建EntityAttribute的实例
        /// </summary>
        public EntityAttribute()
        {
        }
        /// <summary>
        /// 设置数据库表名
        /// </summary>
        /// <param name="tableName">此实体对应的数据表名</param>
        public EntityAttribute(string tableName)
        {
            this.TableName = tableName;
        }
        /// <summary>
        /// 设置数据库表名和连接查询
        /// </summary>
        /// <param name="tableName">此实体对应的数据表名</param>
        /// <param name="joinMethod">连接类型</param>
        /// <param name="joinTarget">要连接的表和连接方式</param>
        public EntityAttribute(string tableName, EJoinMethodBase joinMethod, string joinTarget)
            : this(tableName, joinMethod, null, joinTarget)
        {
        }
        /// <summary>
        /// 设置数据库表名和自定义连接查询
        /// </summary>
        /// <param name="tableName">此实体对应的数据表名</param>
        /// <param name="joinMethod">连接类型</param>
        /// <param name="joinTemplate">连接模板</param>
        /// <param name="joinTarget">要连接的表和连接方式</param>
        public EntityAttribute(string tableName, EJoinMethodBase joinMethod, string joinTemplate, string joinTarget)
        {
            this.TableName = tableName;
            if (joinMethod == EJoinMethodBase.CustomMethod)
                this.JoinMethod = EJoinMethod.CustomMethod(joinTemplate);
            else
                this.JoinMethod = joinMethod;
            this.JoinTarget = joinTarget;
        }

        /// <summary>
        /// 实体对应数据表名
        /// </summary>
        public string TableName { get; set; }
        /// <summary>
        /// 默认连接操作，使用EJoinMethod.CustomMethod()可自定义连接条件
        /// </summary>
        public EJoinMethod JoinMethod { get; set; }
        /// <summary>
        /// 连接的目标，格式为[TargetTableName] on [TableName].[PrimaryKey]=[TargetTableName].[ForeignKey]
        /// </summary>
        public string JoinTarget { get; set; }
    }
}
